package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.vo.TableDataInfoVo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.YhCommissionRecordVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YhCommissionRecord;
import com.ruoyi.system.service.IYhCommissionRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 佣金记录Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "佣金记录管理")
@RestController
@RequestMapping("/yh/commission/record")
public class YhCommissionRecordController extends BaseController
{
    @Autowired
    private IYhCommissionRecordService yhCommissionRecordService;

    /**
     * 查询佣金记录列表
     */
    @ApiOperation("获取佣金记录列表")
    @GetMapping("/list")
    public TableDataInfo list(YhCommissionRecord yhCommissionRecord)
    {
        startPage();
        List<YhCommissionRecord> list = yhCommissionRecordService.list(yhCommissionRecord.queryWrapper());
        return getDataTable(list);
    }
    /**
     * 用户查询自己的佣金记录列表
     */
    @ApiOperation("用户查询自己的佣金记录列表")
    @GetMapping("/listByMember")
    public TableDataInfoVo listByMember(YhCommissionRecord yhCommissionRecord)
    {
        yhCommissionRecord.setMemberId(SecurityUtils.getUserId().toString());
        double sum = yhCommissionRecordService.list(yhCommissionRecord.queryWrapper()).stream()
                .map(YhCommissionRecord::getCommission)
                .collect(Collectors.summarizingDouble(value -> value)).getSum();
        startPage();

        List<YhCommissionRecordVo> list = yhCommissionRecordService.listByMember(yhCommissionRecord);
        TableDataInfo dataTable = getDataTable(list);

        TableDataInfoVo vo = new TableDataInfoVo();
        BeanUtil.copyProperties(dataTable, vo);
        vo.setBackup(sum);
        return vo;
    }

    /**
     * 导出佣金记录列表
     */
    @ApiOperation("导出佣金记录列表")
    @Log(title = "佣金记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhCommissionRecord yhCommissionRecord)
    {
        List<YhCommissionRecord> list = yhCommissionRecordService.list(yhCommissionRecord.queryWrapper());
        ExcelUtil<YhCommissionRecord> util = new ExcelUtil<YhCommissionRecord>(YhCommissionRecord.class);
        util.exportExcel(response, list, "佣金记录数据");
    }

    /**
     * 获取佣金记录详细信息
     */
    @ApiOperation("获取佣金记录详细")
    @ApiImplicitParam(name = "id", value = "佣金记录ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(yhCommissionRecordService.getById(id));
    }

    /**
     * 新增佣金记录
     */
    @ApiOperation("新增佣金记录")
    @Log(title = "佣金记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhCommissionRecord yhCommissionRecord)
    {
        return toAjax(yhCommissionRecordService.save(yhCommissionRecord));
    }

    /**
     * 修改佣金记录
     */
    @ApiOperation("更新佣金记录")
    @Log(title = "佣金记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhCommissionRecord yhCommissionRecord)
    {
        return toAjax(yhCommissionRecordService.updateById(yhCommissionRecord));
    }

    /**
     * 删除佣金记录
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "佣金记录ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:commissionRecord:remove')")
    @Log(title = "佣金记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(yhCommissionRecordService.removeByIds(ids));
    }
}
